package com.example.springdatajpaderivedquerymethod.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.springdatajpaderivedquerymethod.inter.PersonInfo;
import com.example.springdatajpaderivedquerymethod.model.Employee;
import com.example.springdatajpaderivedquerymethod.model.Person;
import com.example.springdatajpaderivedquerymethod.repositories.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/person")
public class PersonController {
    @Autowired
    private PersonRepository personRepository;

    @PostMapping(value = "saveAllPersons",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Void> saveAllPersons(@RequestBody List<Person> persons){
        personRepository.saveAll(persons);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/findAll",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<PersonInfo>> findAll(){
        List<PersonInfo> personInfos = personRepository.findBy();
        personInfos.forEach(personInfo -> {
            String str = personInfo.getDetailedString("Person","Department");
            System.err.println(str);
        });
        return new ResponseEntity<>(personInfos,HttpStatus.OK);
    }

    @GetMapping(value = "/findBySalaryBetween",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<PersonInfo>> findBySalaryBetween(@RequestParam("salaryMin") final Integer salaryMin,
                                                                @RequestParam("salaryMax") final Integer salaryMax){
        List<PersonInfo> personInfos = personRepository.findBySalaryBetween(salaryMin,salaryMax);
        return new ResponseEntity<>(personInfos,HttpStatus.OK);
    }

    @GetMapping(value = "/findByDepartmentLocation",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<PersonInfo>> findByDepartmentLocation(@RequestParam("location") final String location){
        List<PersonInfo> personInfos = personRepository.findByDepartmentLocation(location);
        return new ResponseEntity<>(personInfos,HttpStatus.OK);
    }



}
